package com.gas.gm.pdu;

import org.apache.log4j.Logger;

import com.gas.gm.model.Account;
import com.gas.gm.service.AccountService;
import com.logica.smpp.Data;
import com.logica.smpp.pdu.BindRequest;

public class AccountProcessorValidator implements ProcessorValidator {
	
	private final Logger logger=Logger.getLogger(AccountProcessorValidator.class);
	
	private AccountService service;
	
	
	public AccountProcessorValidator(AccountService service){
		this.service=service;
	}

	public int validate(BindRequest request) {
		int commandStatus=Data.ESME_ROK;
		Account account=service.findFromFile(request.getSystemId());
		if(account!=null){
			String password=account.getPassword();
			if(password!=null){
				if(!request.getPassword().equals(password)){
					commandStatus=Data.ESME_RINVPASWD;
					logger.debug(request.getSystemId()+" authentification failed: invalid password"+commandStatus);
				}else{
					
					logger.info("system id "+request.getSystemId()+" authenticated");
					
				}
			}else{
				commandStatus=Data.ESME_RINVPASWD;
				logger.debug("system id " + request.getSystemId() + " not authenticated. "
						+ "Password attribute not found in users file");
			}
		}else{
			commandStatus = Data.ESME_RINVSYSID;
			logger.debug("system id " + request.getSystemId()
					+ " not authenticated -- not found");
		}
		
		
		return commandStatus;
	}

}
